public class RetrieveLayout {
	private String SessionId {get;set;}
	String serverURL;
	public Integer LayoutColumns{get;set;}
	public List<partnerSoapSforceCom.DescribeLayoutSection> EditSections{get;set;}
	public List<string> LayoutFields{get;set;}
	String ObjectType;
	
	public RetrieveLayout ( ) { 

	} 
	public RetrieveLayout (String objectType, String serverURL ) { 
		this.ObjectType = objectType;
		this.serverURL = serverURL;
	} 
	
	public void Initialise(){
		LayoutFields = new List<string>();
		EditSections = new List<partnerSoapSforceCom.DescribeLayoutSection>();
		SessionId = Userinfo.getSessionId();
		System.debug('Session Id: ' + SessionId );
		
		
		Map<String, String> httpHeaders = ApexPages.currentPage().getHeaders();
		String sfFullHost = null;
		if (httpHeaders.containsKey('Referer')) {
			String referer = httpHeaders.get('Referer');
			Integer sfPart = referer.indexOf('.salesforce.com');
			if (sfPart > 8) {
				sfFullHost = referer.substring(0, sfPart+15);
			}
		}
		if (sfFullHost == null) {
			// No luck with referer, try making it up from parsing Host
			if (httpHeaders.containsKey('Host')) {
				String host = httpHeaders.get('Host').toLowerCase();
					// Should be of form <managed-package-prefix>.<sf-datacentre>.visual.force.com
					// But also test for [<managed-package-prefix>.]<sf-datacentre>.salesforce.com
					// in case they change it!
					if (host.startsWith('lukepkg.')) {
	                        host = host.substring(8);
					}
					if (host.endsWith('.visual.force.com')) {
						sfFullHost = 'https://'+host.substring(0, host.length()-17)+'-api.salesforce.com';
					} else if (host.endsWith('.salesforce.com')) {
						sfFullHost = 'https://'+host;
					}
			}
		}
		
				
		Organization org = [SELECT Id FROM Organization limit 1];
		String sOrgId = org.Id;
		sOrgId = sOrgId.substring(0, sOrgId.length()-3);
		Integer firstdot = sfFullHost.indexOf('.');
		string uptdFullHost = sfFullHost.substring(firstdot+1, sfFullHost.length());
		if(!uptdFullHost.startsWith('salesforce'))
		{
			sfFullHost = uptdFullHost;
		}
		String constuctURL = 'https://' + sfFullHost + '/services/Soap/u/22.0/' + sOrgId;
		System.debug('Full Host:' + sfFullHost);
		partnerSoapSforceCom.Soap sfapi = new partnerSoapSforceCom.Soap();
		//sfapi.endpoint_x = 'https://na12-api.salesforce.com/services/Soap/u/22.0/00DU0000000Irqo';
		if(serverURL == '')
		{
			sfapi.endpoint_x = constuctURL;
		}
		else
		{
			sfapi.endpoint_x = serverURL;
		}		
		partnerSoapSforceCom.SessionHeader_element hdr = new partnerSoapSforceCom.SessionHeader_element();
		hdr.sessionId = SessionId;
		sfapi.SessionHeader = hdr;

		
		System.debug('Object Type Set to:' + ObjectType );
		System.debug('Endpoint Set to:' + serverURL);
		
		partnerSoapSforceCom.DescribeLayoutResult res = sfapi.describeLayout(ObjectType, null);
		
		for(partnerSoapSforceCom.DescribeLayout lay : res.layouts){
			
			for(partnerSoapSforceCom.DescribeLayoutSection sec : lay.editLayoutSections){
				LayoutColumns = sec.columns;
				
				EditSections.add(sec);
				for(partnerSoapSforceCom.DescribeLayoutRow row : sec.layoutRows){
						if(row.layoutItems != null){
							for(partnerSoapSforceCom.DescribeLayoutItem item : row.layoutItems){												
								System.debug('Field Label:' + item.label);
								if(item.layoutComponents !=null){
									for(partnerSoapSforceCom.DescribeLayoutComponent comp :item.layoutComponents){
										if(comp.value != '' && comp.value != null && comp.type_x != 'Separator'){
											System.debug('Field Name:' + comp.value);
											LayoutFields.add(comp.value);
										}
										
									}
								}
							}
						}
				}
			}
		}
	}	
	public static testMethod void testUpsertLeads() {
		RetrieveLayout retLay = new RetrieveLayout('Account','https://na12-api.salesforce.com/services/Soap/u/22.0/00DU0000000Irqo' );
		
		retLay.Initialise();
		
		System.debug('Session Id: ' + retLay.SessionId );

	}	
}